package org.bling.star.server.dispatchers;

import org.bling.star.common.command.CommandType;
import org.bling.star.common.response.BaseResponse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StrategyFactory {
    private final Map<Integer, ServiceStrategy> strategyMap = new HashMap<>();

    public StrategyFactory(List<ServiceStrategy> strategies) {
        for (ServiceStrategy strategy : strategies) {
            Class<?>[] interfaces = strategy.getClass().getInterfaces();
            for (Class<?> iface : interfaces) {
                if (iface.isAnnotationPresent(Command.class)) {
                    Command annotation = iface.getAnnotation(Command.class);
                    strategyMap.put(annotation.value(), strategy);
                }
            }
        }
    }

    public ServiceStrategy getStrategy(CommandType commandType) {
        return strategyMap.getOrDefault(commandType.getCode(), (obj,connect) -> new BaseResponse(null));
    }
}
